package com.lijianan.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lijianan.entity.BookingEquipment;
import com.lijianan.entity.MultimediaEquipment;
import com.lijianan.mapper.BookingEquipmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BookingEquipmentServiceImpl implements BookingEquipmentService {

    @Autowired
    private BookingEquipmentMapper bookingEquipmentMapper;

    @Autowired
    private MultimediaEquipmentServiceImpl multimediaEquipmentServiceImpl;

    @Override
    public List<MultimediaEquipment> selectMultimediaEquipmentsByBookingId(Integer booking_id) {
        List<MultimediaEquipment> multimediaEquipments = new ArrayList<>();
        List<BookingEquipment> bookingEquipments = selectByBookingId(booking_id);
        for (BookingEquipment bookingEquipment : bookingEquipments) {
            MultimediaEquipment multimediaEquipment = multimediaEquipmentServiceImpl.selectByEquipmentId(bookingEquipment.getEquipment_id());
            multimediaEquipments.add(multimediaEquipment);
        }
        return multimediaEquipments;
    }

    @Override
    public boolean addBookingEquipment(Integer booking_id, Integer equipment_id) {
        QueryWrapper<BookingEquipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("booking_id", booking_id).eq("equipment_id", equipment_id);
        if (bookingEquipmentMapper.selectOne(queryWrapper) != null) {
            System.out.println("已经存在该设备请求！");
            return false;
        }
        BookingEquipment bookingEquipment = new BookingEquipment();
        bookingEquipment.setBooking_id(booking_id);
        bookingEquipment.setEquipment_id(equipment_id);
        return bookingEquipmentMapper.insert(bookingEquipment) != 0;
    }

    @Override
    public boolean updateBookingEquipment(Integer booking_id, String name) {
        MultimediaEquipment multimediaEquipment = multimediaEquipmentServiceImpl.selectByName(name);
        return addBookingEquipment(booking_id, multimediaEquipment.getEquipment_id());
    }

    @Override
    public List<BookingEquipment> selectByBookingId(Integer booking_id) {
        QueryWrapper<BookingEquipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("booking_id", booking_id);
        return bookingEquipmentMapper.selectList(queryWrapper);
    }

    @Override
    public BookingEquipment selectById(Integer id) {
        return bookingEquipmentMapper.selectById(id);
    }

    @Override
    public List<BookingEquipment> selectAll() {
        return bookingEquipmentMapper.selectList(null);
    }

    @Override
    public boolean deleteBookingEquipment(Integer id) {
        return bookingEquipmentMapper.deleteById(id) != 0;
    }
}
